<?php
// Kalenterin otsikot
include("calendarheaders.php");

// Hae päämuuttujat
$monthview = "false";
$newmonthview = "true";
if(strcmp($_GET["monthview"],"true") == 0)
{
    $monthview = "true";
    $newmonthview = "false";
}

if(is_numeric($_GET["offset"]))
    $offset = $_GET["offset"];
else
    $offset = 0;

// Kuukausi- ja viikkonäkymä
if(strcmp($newmonthview,"true") == 0)
{
    // Laske kuukausi-offset viikko-offsetistä
    $newoffset = time() + $offset * 7 * 24 * 60 * 60;
    $newoffset = date("n",$newoffset) - date("n") + 12 * (date("Y",$newoffset) - date("Y"));
}
if(strcmp($monthview,"false") == 0)
    $entries = $Calendar->get_entries($user,$offset);

?>
<div id="content">
    <h2>Kalenteri</h2>
    <div id="calendar">
        <nav>
            <span class="leftalign"><a href="?page=calendar&monthview=<? echo $monthview; ?>&offset=<? echo ($offset - 1); ?>">Edellinen</a></span>
            <? if(strcmp($newmonthview,"true") == 0) { ?>
                <a href="?page=calendar&monthview=<? echo $newmonthview; ?>&offset=<? echo $newoffset; ?>">Kuukausinäkymä</a> | 
            <? } ?>
            <a href="?page=calendar">Nykyinen viikko</a>
            <span class="rightalign"><a href="?page=calendar&monthview=<? echo $monthview; ?>&offset=<? echo ($offset + 1); ?>">Seuraava</a></span>
        </nav>
        <p>
            <img src="calendarimg.php?monthview=<? echo $monthview; ?>&offset=<? echo $offset; ?>" alt="Kalenteri" usemap="#calendarmap" />
            <map name="calendarmap">
                <?
                if(strcmp($monthview,"false") == 0)
                {
                    $i = -1;
                    foreach($entries as &$entry)
                    {
                        ++$i;
                        
                        $start_time = $entry->start_time;
                        $end_time = $entry->end_time;
                        
                        $text = e($entry->course) . "<br />" . date("H:i",$start_time) . "—" . date("H:i",$end_time) . "<br />" . e($entry->location);

                        $x1 = date("N",$start_time) * COLUMN_WIDTH;
                        $x2 = $x1 + COLUMN_WIDTH;
                        $y1 = 2 * HEADER_HEIGHT + date("H",$start_time) * HOUR_HEIGHT + date("i",$start_time) * 60 / HOUR_HEIGHT;
                        $y2 = 2 * HEADER_HEIGHT + date("H",$end_time) * HOUR_HEIGHT + date("i",$end_time) * 60 / HOUR_HEIGHT;
                        
                        ?>
                        <area class="tooltip-target" id="tooltip-target-<? echo $i; ?>" shape="rect" coords="<? echo $x1; ?>,<? echo $y1; ?>,<? echo $x2; ?>,<? echo $y2; ?>" href="javascript:void(0);" alt="<? echo $text; ?>" />
                        <div class="tooltip-content" id="tooltip-content-<? echo $i; ?>"><? echo $text; ?></div>
                        <?
                    }
                }
                else
                {
                    $week_date = mktime(date("H"),date("i"),date("s"),date("n")+$offset,1);
                    while(date("N",$week_date) < date("N")) $week_date += 24 * 60 * 60;
                    while(date("N",$week_date) > date("N")) $week_date -= 24 * 60 * 60;
                    for($y = 1; $y < 7; ++$y)
                    {
                        $week = 1 * date("W",$week_date);
                        $newoffset = floor(($week_date - time()) / (7 * 24 * 60 * 60));
                        ?>
                            <area shape="rect" coords="0,<? echo $y * CELL_HEIGHT; ?>,<? echo 8 * CELL_WIDTH; ?>,<? echo ($y + 1) * CELL_HEIGHT; ?>" href="?page=calendar&monthview=false&offset=<? echo $newoffset ?>" alt="Viikko <? echo $week; ?>" title="Viikko <? echo $week; ?>" />
                        <?
                        $week_date += 7 * 24 * 60 * 60;
                    } 
                }
                ?>
            </map>
        </p>
    </div>
</div>
